//
//  CImageUtil.m
//  PhotoMask
//
//  Created by 为为 on 13-4-15.
//  Copyright (c) 2013年 为为. All rights reserved.
//

#import "CImageUtil.h"
#import <QuartzCore/QuartzCore.h>

@implementation CImageUtil

+(UIImage *)imageFromFile:(NSString *)fileName
{
	NSString* path;
	path = [[NSBundle mainBundle] pathForResource:fileName ofType:NULL];
	
	UIImage* image;
	image = [UIImage imageWithContentsOfFile:path];
	
	return image;
}

+(UIImage*)imageFromImage:(UIImage*)image inRect:(CGRect)rect {
	CGImageRef imageRef = CGImageCreateWithImageInRect(image.CGImage, rect);
	UIImage *subImage = [UIImage imageWithCGImage:imageRef];
	CGImageRelease(imageRef);
	
	return subImage;
}

+ (UIImage *)imageFromImage:(UIImage *)image scaleToSize:(CGSize)reSize {
    UIGraphicsBeginImageContextWithOptions(reSize, NO, 0.0f);
	[image drawInRect:CGRectMake(0, 0, reSize.width, reSize.height)];
	UIImage *reSizeImage = UIGraphicsGetImageFromCurrentImageContext();
	UIGraphicsEndImageContext();
	
	return reSizeImage;
}

+(UIImage*)imageFromView:(UIView *)theView {
	CGRect rect = theView.bounds;
//	UIGraphicsBeginImageContext(rect.size);
    UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0.0f);
	CGContextRef context =UIGraphicsGetCurrentContext();
	[theView.layer renderInContext:context];
	UIImage *img =UIGraphicsGetImageFromCurrentImageContext();
	UIGraphicsEndImageContext();
	return img;
}

+(UIImage *)combineImage:(UIImage *)desImg fromImage:(UIImage *)srcImg andMask:(UIImage*)maskImage
{
	CGRect rect = CGRectMake(0, 0, desImg.size.width, desImg.size.height);
	CGColorSpaceRef cref = CGColorSpaceCreateDeviceRGB();
    CGContextRef gc = CGBitmapContextCreate(NULL,
                                            rect.size.width,
											rect.size.height,
                                            8,rect.size.width*4,
                                            cref,kCGImageAlphaPremultipliedLast);
    CGColorSpaceRelease(cref);
	
	CGContextDrawImage(gc, rect, desImg.CGImage);
	CGContextClipToMask(gc, rect, maskImage.CGImage);
    CGContextDrawImage(gc, rect, srcImg.CGImage);
    
	CGImageRef imgRef = CGBitmapContextCreateImage(gc);
    UIImage *resultImage= [UIImage imageWithCGImage:imgRef];
	CGImageRelease(imgRef);
	
	CGContextRelease(gc);
    
    return resultImage;
}

+ (CGRect)zoomRect:(CGRect)rect withScale:(CGFloat)scale
{
	CGPoint center = CGPointMake(rect.origin.x+rect.size.width/2, rect.origin.y+rect.size.height/2);
	CGFloat width = rect.size.width*scale;
	CGFloat height = rect.size.height*scale;
	CGPoint origin = CGPointMake(center.x-width/2, center.y-height/2);
	rect.origin = origin;
	rect.size = CGSizeMake(width, height);
	
	return rect;
}

+(CGRect)ratioScaleSize:(CGSize)size toRect:(CGRect)rect {
	double xRatio = (double)rect.size.width/size.width;
	double yRatio = (double)rect.size.height/size.height;
	double fRatio = xRatio>yRatio?yRatio:xRatio;
	
	CGSize fSize = CGSizeMake(size.width*fRatio, size.height*fRatio);
	CGRect fRect = CGRectMake(rect.origin.x+rect.size.width/2-fSize.width/2,
							  rect.origin.y+rect.size.height/2-fSize.height/2,
							  fSize.width,
							  fSize.height);
	
	return fRect;
}

+ (CGRect)ratioScaleSize:(CGSize)size overRect:(CGRect)rect
{
	double xRatio = (double)rect.size.width/size.width;
	double yRatio = (double)rect.size.height/size.height;
	double fRatio = xRatio<yRatio?yRatio:xRatio;
	
	CGSize fSize = CGSizeMake(size.width*fRatio, size.height*fRatio);
	CGRect fRect = CGRectMake(rect.origin.x+rect.size.width/2-fSize.width/2,
							  rect.origin.y+rect.size.height/2-fSize.height/2,
							  fSize.width,
							  fSize.height);
	
	return fRect; 	
}

+ (CGRect)rectFromSize:(CGSize)size withRectRatio:(CGRect)rect
{
	double sizeRatio = size.height/size.width;
	double rectRatio = rect.size.height/rect.size.width;
	double fwidth;
	double fheight;
	if (sizeRatio > rectRatio) {
		fwidth = size.width;
		fheight = size.width*rectRatio;
	} else {
		fwidth = size.height/rectRatio;
		fheight = size.height;
	}
	CGRect frect = CGRectMake(size.width/2-fwidth/2, size.height/2-fheight/2, fwidth, fheight);
	return frect;
}

+(UIImage *)imageFromImage:(UIImage*)image orName:(NSString*)name withTintColor:(UIColor*)tintColor
{
    UIImage *baseImage = nil;
    
    baseImage = image?image:(name?[UIImage imageNamed:name]:nil);
    
    if (!baseImage) {
        return nil;
    }
    
    CGRect drawRect = CGRectMake(0, 0, baseImage.size.width, baseImage.size.height);
    
    UIGraphicsBeginImageContextWithOptions(baseImage.size, NO, 0);
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGContextTranslateCTM(context, 0, baseImage.size.height);
    CGContextScaleCTM(context, 1.0, -1.0);
    
    // draw original image
    CGContextSetBlendMode(context, kCGBlendModeNormal);
    CGContextDrawImage(context, drawRect, baseImage.CGImage);
    
    // draw color atop
    CGContextSetFillColorWithColor(context, tintColor.CGColor);
    CGContextSetBlendMode(context, kCGBlendModeSourceAtop);
    CGContextFillRect(context, drawRect);
    
    UIImage *tintedImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return tintedImage;
}

@end
